/* 

auth：王宇嘉
date：2022-8-20
desc：uniapp 原生请求方式向后端发请求

 */

class HandleOdooApiBase {
	constructor(func, baseApi = 'list') {
		this.baseApi = baseApi // 库的名字
		this.func = func

		this.list = [] // 用于接收查询结果
		this.item = {} // 用于接收查询结果
		this.loading = false // 是否是加载中。
		this.state = true // 请求结果状态。成功true，失败false
		this.total = 0
		this.others = {}
	}
	async getList(listQuery) {
		const context = this
		this.listLoading = true

		await this.func[this.baseApi](listQuery)
			.then(function(res) {
				context.list = 'result' in res.data ? res.data.result : res.data
				context.total = 'count' in res.data ? res.data.count : res.data.length
				// eslint-disable-next-line no-unused-vars
				const {
					count,
					limit,
					offset,
					results,
					...others
				} = res.data
				context.others = 'count' in res.data ? others : {}
				setTimeout(function() {
					context.listLoading = false
				}, 100)
			})
			.catch(function() {
				setTimeout(function() {
					context.listLoading = false
				}, 100)
			})
	}
	async getItem(id) {
		const context = this
		this.detailLoading = true
		// this.item = {}
		await this.func.retrieve(id)
			.then(function(res) {
				context.item = 'result' in res.data ? res.data.result : res.data
				setTimeout(function() {
					context.detailLoading = false
				}, 100)
			})
			.catch(function() {
				setTimeout(function() {
					context.detailLoading = false
				}, 100)
			})
	}
}

export {
	HandleOdooApiBase
}
